﻿using System;
using System.IO;
using System.Collections.Generic;
using Microsoft.Data.Schema.ScriptDom.Sql;
using System.Linq;
using System.Text;

namespace Neznayka
{
    internal class VariableAssignmentVisitor : TSqlConcreteFragmentVisitor
    {
        private List<Literal> _targets;

        #region ctor
        public VariableAssignmentVisitor()
        {
            _targets = new List<Literal>();
        }
        #endregion

        #region properties
        public List<Literal> VariableAssignments
        {
            get  {  return _targets;  }
        }
        #endregion

        #region overrides
        public override void ExplicitVisit(ExecuteParameter node)
        {
            if (node.IsOutput == true)
            {
                Literal PV = node.ParameterValue as Microsoft.Data.Schema.ScriptDom.Sql.Literal;
                if (PV != null && PV.LiteralType == LiteralType.Variable)
                {
                    _targets.Add(PV);
                }
            }
        }
        public override void ExplicitVisit(SetVariableStatement node)
        {
            if (node.VariableName != null && node.VariableName.LiteralType == LiteralType.Variable)
            {
                _targets.Add(node.VariableName);
            }
        }
        public override void ExplicitVisit(SelectSetVariable node)
        {
            if (node.VariableName != null && node.VariableName.LiteralType == LiteralType.Variable)
            {
                _targets.Add(node.VariableName);
            }
        }
        public override void ExplicitVisit(ExecuteStatement node)
        {
//            if (node.Variable != null && node.Variable.LiteralType == LiteralType.Variable && node.Variable.Value != "")
            if (node.Variable != null && node.Variable.LiteralType == LiteralType.Variable && ! String.IsNullOrEmpty(node.Variable.Value))
                {
                _targets.Add(node.Variable);
            }
            node.AcceptChildren(this);
        }
        public override void ExplicitVisit(AssignmentSetClause node)
        {
            if (node.Variable != null && node.Variable.LiteralType == LiteralType.Variable)
            {
                _targets.Add(node.Variable);
            }
        }
        public override void ExplicitVisit(FetchCursorStatement node)
        {
            if (node.IntoVariables != null )
            {
                foreach (var v in node.IntoVariables)
                {
                    if (v.LiteralType == LiteralType.Variable)
                    {
                        _targets.Add(v);
                    }
                }
            }
        }
        public override void ExplicitVisit(BeginDialogStatement node)
        {
            if (node.Handle != null && node.Handle.LiteralType == LiteralType.Variable)
            {
                _targets.Add(node.Handle);
            }
        }
        public override void ExplicitVisit(ReceiveStatement node)
        {
            if (node.SelectElements != null)
            {
                foreach (var v in node.SelectElements)
                {
                    SelectSetVariable ssv = v as SelectSetVariable;
                    if (ssv != null)
                    {
                        _targets.Add(ssv.VariableName);
                    }
                }
            }
        }

        #endregion

    }
}

